/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scribe.system.peer.scribe;

import tman.system.peer.tman.*;
import common.peer.PeerAddress;
import java.util.Comparator;
import se.sics.asdistances.ASDistances;

/**
 *
 * @author gianmariospacagna
 */
public class DistanceASComparator implements Comparator<PeerAddress> {

    private PeerAddress ref;

    public DistanceASComparator(PeerAddress ref) {
        this.ref = ref;
    }

    @Override
    public int compare(PeerAddress t, PeerAddress t1) {
        ASDistances distances = ASDistances.getInstance();
        int distT = distances.getASFromIP(t.getPeerAddress().getIp().getHostAddress());
        int distT1 = distances.getASFromIP(t1.getPeerAddress().getIp().getHostAddress());
        int distRef = distances.getASFromIP(ref.getPeerAddress().getIp().getHostAddress());
        
        if (Math.abs(distT-distRef)<Math.abs(distT1-distRef)) {
            return -1;
        } else if (Math.abs(distT-distRef)==Math.abs(distT1-distRef)) {
            return 0;
        } else {
            return 1;
        }

    }
}
